package cpu;

import sun.misc.Contended;

import java.util.concurrent.CountDownLatch;

public class CacheLineTest {
    private static long COUNT = 20_0000_0000L;

    private static class T {
//      private long x1, x2, x3, x4, x5, x6, x7;
        @Contended
        private long x = 0L;
//      private long x9, x10, x11, x12, x13, x14, x15;
    }

    private static T[] arr = new T[2];

    static {
        arr[0] = new T();
        arr[1] = new T();
    }

    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);
        Thread t1 = new Thread(() -> {
            for (int j = 0; j < COUNT; j++) {
                arr[0].x = j;
            }
            latch.countDown();
        });
        Thread t2 = new Thread(() -> {
            for (int j = 0; j < COUNT; j++) {
                arr[1].x = j;
            }
            latch.countDown();
        });
        final long start = System.nanoTime();
        t1.start();
        t2.start();
        latch.await();
        System.out.println((System.nanoTime() - start) / 100_0000);
    }
}
